﻿#include "ssRenderSystem.h"
#include "../node/ssNode.h"
#include "../element/render/ssMeshRender.h"
#include "../element/material/ssMaterial.h"


namespace StarSeeker
{

ssRenderSystem::ssRenderSystem()
{
	m_compType.Insert(ssComponent::ComponentType_e::SS_COMPONENT_RENDER);
	m_compType.Insert(ssComponent::ComponentType_e::SS_COMPONENT_MATERIAL);
}

void ssRenderSystem::Work()
{
	ssMeshRender* pRender = 0;
	ssMaterial* pMaterial = 0;
	for (u32 i = 0; i < m_background.Count(); ++i)
	{
		pRender = SS_DYNAMIC_CAST(ssMeshRender*, m_background[i]->GetComponent(ssComponent::SS_COMPONENT_RENDER));
		pMaterial = SS_DYNAMIC_CAST(ssMaterial*, m_background[i]->GetComponent(ssComponent::SS_COMPONENT_MATERIAL));
		pMaterial->SwitchShader();//必须先调用设置当前shader
		pMaterial->OnRenderBegin();
		pRender->OnRenderBegin();
		pRender->Render();
		pRender->OnRenderEnd();
		pMaterial->OnRenderEnd();
	}
	for (u32 i = 0; i < m_geomery.Count(); ++i)
	{
		pRender = SS_DYNAMIC_CAST(ssMeshRender*, m_geomery[i]->GetComponent(ssComponent::SS_COMPONENT_RENDER));
		pMaterial = SS_DYNAMIC_CAST(ssMaterial*, m_geomery[i]->GetComponent(ssComponent::SS_COMPONENT_MATERIAL));
		pMaterial->SwitchShader();//必须先调用设置当前shader
		pMaterial->OnRenderBegin();
		pRender->OnRenderBegin();
		pRender->Render();
		pRender->OnRenderEnd();
		pMaterial->OnRenderEnd();
	}
	glDepthMask(GL_FALSE);
	for (u32 i = 0; i < m_alpha.Count(); ++i)
	{
		pRender = SS_DYNAMIC_CAST(ssMeshRender*, m_alpha[i]->GetComponent(ssComponent::SS_COMPONENT_RENDER));
		pMaterial = SS_DYNAMIC_CAST(ssMaterial*, m_alpha[i]->GetComponent(ssComponent::SS_COMPONENT_MATERIAL));
		pMaterial->SwitchShader();//必须先调用设置当前shader
		pMaterial->OnRenderBegin();
		pRender->OnRenderBegin();
		pRender->Render();
		pRender->OnRenderEnd();
		pMaterial->OnRenderEnd();
	}
	for (u32 i = 0; i < m_overlay.Count(); ++i)
	{
		pRender = SS_DYNAMIC_CAST(ssMeshRender*, m_overlay[i]->GetComponent(ssComponent::SS_COMPONENT_RENDER));
		pMaterial = SS_DYNAMIC_CAST(ssMaterial*, m_overlay[i]->GetComponent(ssComponent::SS_COMPONENT_MATERIAL));
		pMaterial->SwitchShader();//必须先调用设置当前shader
		pMaterial->OnRenderBegin();
		pRender->OnRenderBegin();
		pRender->Render();
		pRender->OnRenderEnd();
		pMaterial->OnRenderEnd();
	}
	glDepthMask(GL_TRUE);
}

void ssRenderSystem::Empty()
{
	m_background.Empty();
	m_geomery.Empty();
	m_alpha.Empty();
	m_overlay.Empty();
}

void ssRenderSystem::CheckNode(ssNode * node)
{
	ssMeshRender* pRender = SS_DYNAMIC_CAST(ssMeshRender*, node->GetComponent(ssComponent::SS_COMPONENT_RENDER));
	ssMaterial* pMaterial = SS_DYNAMIC_CAST(ssMaterial*, node->GetComponent(ssComponent::SS_COMPONENT_MATERIAL));
	if (pRender && pRender->m_enable && pMaterial)
	{
		//m_nodes.PushBack(node);
		if (pRender->m_queTag < SS_QUE_GEOMERY)
		{
			m_background.PushBackCP(node);
		}
		else if (pRender->m_queTag < SS_QUE_ALPHA)
		{
			m_geomery.PushBackCP(node);
		}
		else if (pRender->m_queTag < SS_QUE_OVERLAY)
		{
			m_alpha.PushBackCP(node);
		}
		else
		{
			m_overlay.PushBackCP(node);
		}
	}
}

}
